define(function (require) {
    var doT = require('doT');
    require('treegrid');
    require('bootstrapValidator');
    require('messager');

    $.module("SYS.sys_menu", function () {
        return {
            init: function () {
                this.loadData();
                this.loadEvent();
            },
            loadData: function () {
                SYS.Core.ajax({
                    url: 'sys/sysMenuList',
                    common: {},
                    data: {},
                    success: function (data) {
                        var template = doT.template($("#tmpl_sys_menu_list").html());
                        $("#sys_menu_list").html(template(data.data));
                        $('#menu_tree').treegrid();
                        $('#menu_tree').treegrid('collapseAll');
                    }
                })
            },
            loadEvent: function () {
                var that = this;
                $('#fm').bootstrapValidator({
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        menu_name: {
                            validators: {
                                notEmpty: {
                                    message: '此项必填'
                                }
                            }
                        },
                        menu_sort: {
                            validators: {
                                notEmpty: {
                                    message: '此项必填'
                                },
                                regexp: {
                                    regexp: /^[1-9][0-9]*$/,
                                    message: '请输入正整数'
                                }
                            }
                        }
                    },
                    //提交后，整个页面会刷新
                    //submitHandler: function(validator, form, submitButton) {
                    //    that.saveMenu();
                    //}
                }).on('success.form.bv', function (e) { //使用此方法可以ajax
                    e.preventDefault();
                    //保存菜单
                    that.saveMenu();
                });
                //清空验证信息(必须)
                $('#myModal').on('hide.bs.modal', function() {
                    $("#fm input[name='_id']").val('');
                    $("#fm input[name='parent_id']").val('');
                    $("#fm input[name='menu_name']").val('');
                    $("#fm input[name='menu_path']").val('');
                    $("#fm input[name='menu_sort']").val('');
                    $("#fm input[name='icon_cls']").val('');
                    $("#desc").val('');
                    $('#fm').bootstrapValidator('resetForm', true);
                });
            },
            editDialog: function (id) {
                //根据id获取菜单信息
                SYS.Core.ajax({
                    url: "sys/getMenuById",
                    common: {},
                    data: {id: id },
                    success: function (data) {
                        if(data.code == 1){
                            //填充表单
                            var menu = data.data;
                            $("#fm input[name='_id']").val(menu.id);
                            $("#fm input[name='parent_id']").val(menu.parent_id);
                            $("#fm input[name='menu_name']").val(menu.menu_name);
                            $("#fm input[name='menu_path']").val(menu.menu_path);
                            $("#fm input[name='menu_sort']").val(menu.menu_sort);
                            $("#fm input[name='icon_cls']").val(menu.icon_cls);
                            $("#desc").val(menu.desc);
                        }else{
                            $.messager.popup('加载数据失败，请稍后再试','error');
                        }
                    }
                });
                $('#myModal').modal({backdrop: 'static', keyboard: false});
                $('#myModal').modal('show');
            },
            saveMenu: function () {
                var that = this;
                var url = '';
                var id = $("#fm input[name='_id']").val();
                if(id==''){
                    url = 'sys/saveSysMenu';
                }else{
                    url = 'sys/editSysMenu';
                }
                var parent_id = $("#fm input[name='parent_id']").val();
                var menu_name = $("#fm input[name='menu_name']").val();
                var menu_path = $("#fm input[name='menu_path']").val();
                var menu_sort = $("#fm input[name='menu_sort']").val();
                var icon_cls = $("#fm input[name='icon_cls']").val();
                var desc = $("#desc").val();
                SYS.Core.ajax({
                    url: url,
                    common: {},
                    data: {
                        id: id ? id : 0,
                        parent_id: parent_id ? parent_id : 0,
                        menu_name: menu_name,
                        menu_path: menu_path,
                        menu_sort: menu_sort,
                        icon_cls: icon_cls,
                        desc: desc
                    },
                    success: function (data) {
                        if(data.code == 1){
                            $('#myModal').modal('hide');
                            $.messager.popup('保存成功','success');
                            that.loadData();
                        }else{
                            $('#fm').bootstrapValidator('disableSubmitButtons', false);
                            $.messager.popup('保存失败','error');
                        }
                    }
                })
            },
            addChildMenu: function (id) {
                $("#fm input[name='parent_id']").val(id); //绑定父菜单ID
                $('#myModal').modal('show');
            },
            deleteMenu: function (id) {
                var that = this;
                $.messager.confirm('确定要删除选择的菜单吗？如果存在子菜单，将一并删除？', function () {
                    SYS.Core.ajax({
                        url: "sys/delSysMenu",
                        common: {},
                        data: {id: id},
                        success: function (data) {
                            if(data.code == 1){
                                $.messager.popup(data.msg, 'success');
                                that.loadData();
                            }else{
                                $.messager.popup('删除失败，请稍后再试', 'error');
                            }
                        }
                    })
                });
            }
        }
    });
    SYS.sys_menu.init();
});
